package commonservice

import (
	"admin100/app/dao/centerdao"
	"admin100/app/dao/chargedao"
	"admin100/app/dao/gamedao"
	"admin100/app/model/gamemodel"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/define/defLang"
	"strings"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

var GamePlayer gamePlayerService

type gamePlayerService struct{}

// return serverId and playerName
func (s *gamePlayerService) SplitPlayerName(nickName string) (serverId string, playername string) {
	arr := strings.Split(nickName, ".")
	if len(arr) == 2 {
		return arr[0], arr[1]
	} else {
		defErr.Panic(define.RoleNameParseFail, defLang.GotMsg(define.RoleNameParseFail)+" : "+nickName)
	}
	return "", ""
}

//为匹配原项目，暂时不改方法名
func (s *gamePlayerService) GetPlayerByPlatformIdAndNickname(node, nickname string) *gamemodel.Player {
	if nickname == "" {
		defErr.Panic(define.RoleNotExist)
	}

	serverId, name := s.SplitPlayerName(nickname)
	gameDb := gamedao.NewPlayerDao(node)
	one, err := gameDb.WhereValNotEmpty(g.Map{
		gameDb.Columns.ServerId: serverId,
		gameDb.Columns.Nickname: name,
	}).FindOne()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	if one == nil {
		defErr.Panic(define.DbRoleNotExist)
	}

	one.Nickname = one.ServerId + "." + one.Nickname
	globalAccount := centerdao.GlobalAccount.GetGlobalAccount(one.ServerId, one.AccId)
	if globalAccount != nil {
		one.Type = globalAccount.Type
		one.AccountForbidTime = globalAccount.ForbidTime
		one.AccountForbidType = globalAccount.ForbidType
	}
	ppGameDb := gamedao.NewPlayerPropDao(node)
	one.Ingot = ppGameDb.GetPlayerIngot(int(one.Id))

	return one
}

func (s *gamePlayerService) TranPlayerNameSting2PlayerIdList(node, platformId string, playerName string) []int {
	playerIdList := make([]int, 0)
	nameList := strings.Split(playerName, ",")
	for _, name := range nameList {
		player := s.GetPlayerByPlatformIdAndNickname(node, name)
		if player != nil {
			playerIdList = append(playerIdList, int(player.Id))
		}
	}

	return playerIdList
}

//获取当前在线人数
func (s *gamePlayerService) GetNowOnlineCount2(node, serverId string, channelList []string) int {
	gameDb := gamedao.NewPlayerDao(node)
	i, err := gameDb.Where(g.Map{
		gameDb.Columns.IsOnline: 1,
		gameDb.Columns.ServerId: serverId,
		gameDb.Columns.Channel:  channelList,
	}).Count()
	defErr.SelectRetPanic(err, define.DataOptFail, define.DataOptFail)

	return i
}

func (s *gamePlayerService) GetPlayerOne(node, platformId, serverId string, id int) *gamemodel.Player {
	gameplayer := gamedao.NewPlayerDao(node)
	player, err := gameplayer.FindOne(gameplayer.Columns.Id, id)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	if player == nil {
		return nil
	}

	ga := centerdao.GlobalAccount.GetGlobalAccount(platformId, player.AccId)
	if ga == nil {
		return nil
	}
	player.Type = ga.Type
	player.AccountForbidType = ga.ForbidType
	player.AccountForbidTime = ga.ForbidTime

	return player
}

func (s *gamePlayerService) GetPlayerList(node string, req *gamemodel.PlayerListReq) ([]*gamemodel.Player, int) {
	var (
		gameDb = gamedao.NewPlayerDao(node)
		column = gameDb.Columns
	)

	timeSlice := []string{}
	if req.StartTime > 0 {
		timeSlice = []string{gconv.String(req.StartTime), gconv.String(req.EndTime)}
	}

	m := gameDb.WhereValNotEmpty(g.Map{
		column.ServerId:                     req.ServerId,
		column.AccId:                        req.Account,
		column.LastLoginIp:                  req.Ip,
		column.Nickname:                     req.Nickname,
		column.IsOnline:                     req.IsOnline,
		column.Id:                           req.PlayerId,
		column.RegTime + " between ? and ?": timeSlice,
		column.Channel:                      strings.Split(req.ChannelList, ","),
	})

	m2 := m.Clone()
	m = m.Fields("player.*, player_data.level, player_vip.level as vip_level, player_data.power").
		LeftJoin("player_data", "player_data", "player.id = player_data.player_id").
		LeftJoin("player_vip", "player_vip", "player.id = player_vip.player_id")
	m.M = m.M.Order("id asc")
	data, err := m.Page(req.Cur, req.PNum).FindAll()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	total, err := m2.Count()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	for _, e := range data {
		e.Nickname = e.ServerId + "." + e.Nickname
		ppGameDb := gamedao.NewPlayerPropDao(node)
		e.Ingot = ppGameDb.GetPlayerIngot(int(e.Id))
		playerChargeData := chargedao.PlayerChargeInfoRecord.GetPlayerChargeDataOne(int(e.Id))
		if playerChargeData != nil {
			e.TotalChargeMoney = int(playerChargeData.TotalMoney)
		}
		globalAccount := centerdao.GlobalAccount.GetGlobalAccount(req.PlatformId, req.Account)
		if globalAccount != nil {
			e.Type = globalAccount.Type
			e.AccountForbidType = globalAccount.ForbidType
			e.AccountForbidTime = globalAccount.ForbidTime
			e.Promote = globalAccount.Promote
		}
	}

	return data, total
}
